<template>
    <div class="app-container">

        <el-form :inline="true" class="demo-form-inline caozuo">


            <el-form-item label="公告优先级">
                <el-select v-model="Pvalue" placeholder="请选择">
                    <el-option v-for="item in prioritys" :key="item.Pvalue" :label="item.label" :value="item.Pvalue">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="公告对象">
                <el-select v-model="TSvalue" placeholder="请选择">
                    <el-option v-for="item in TeaOrStu" :key="item.TSvalue" :label="item.label" :value="item.TSvalue">
                    </el-option>
                </el-select>
            </el-form-item>

            <!-- 公告标题 -->
            <el-form-item>
                <el-input  v-model="searchObj.title" placeholder="请输入公告标题" />
            </el-form-item>

            <el-form-item label="公告时间范围">
                <!-- el-date-picker这是时间选择框 -->
                <!-- v-model进行数据绑定 -->
                <el-date-picker type="datetime" v-model="searchObj.beginTime" placeholder="选择开始时间"
                    value-format="yyyy-MM-dd HH:mm:ss" default-time="00:00:00" />
            </el-form-item>
            <el-form-item>
                <el-date-picker type="datetime" v-model="searchObj.endTime" placeholder="选择截止时间"
                    value-format="yyyy-MM-dd HH:mm:ss" default-time="00:00:00" />
            </el-form-item>


            <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
            <el-button type="default" @click="resetData()">清空</el-button>
        </el-form>

        <!-- 表格 -->
        <el-table v-loading="listLoading" :data="list" element-loading-text="数据加载中" border fit highlight-current-row
            row-class-name="myClassList">

            <el-table-column label="序号" width="70" align="center">
                <template slot-scope="scope">
                    {{ (page - 1) * limit + scope.$index + 1 }}
                </template>
            </el-table-column>

            <el-table-column label="公告图片" align="center">
                <template slot-scope="scope">
 
                    <div class="pic">
                        <img :src="scope.row.image" alt="scope.row.title" width="150px">
                    </div>
                </template>
            </el-table-column>


            <el-table-column label="公告标题" align="center">
                <template slot-scope="scope">
                    {{ scope.row.title }}
                </template>
            </el-table-column>

            <el-table-column label="优先级" align="center">
                <template slot-scope="scope">
                    <i v-if="scope.row.priority === 1">一级公告</i>
                    <i v-else-if="scope.row.priority === 2">二级公告</i>
                    <i v-else-if="scope.row.priority === 3">三级公告</i>
                </template>
            </el-table-column>

            <el-table-column label="公告类型" align="center">
                <template slot-scope="scope">
                    <i v-if="scope.row.userType === 0">面向老师</i>
                    <i v-else-if="scope.row.userType === 1">面向学生</i>
                    <i v-else-if="scope.row.userType === 2">老师学生</i>
                </template>
            </el-table-column>

            <el-table-column label="发布时间" width="100" align="center">
                <template slot-scope="scope">
                    {{ scope.row.gmtCreate }}
                </template>
            </el-table-column>



            <el-table-column label="操作" width="150" align="center">
                <template slot-scope="scope">

                    <span @click="editAnnoInfo(scope.row.id)">
                        <el-button type="text" size="mini" icon="el-icon-edit">
                            编辑公告信息
                        </el-button>
                    </span>

                    <span @click="selectAnnoInfo(scope.row.id)">
                        <el-button type="text" size="mini" icon="el-icon-edit">查看公告信息</el-button>
                    </span>

                    <el-button type="text" size="mini" icon="el-icon-delete" @click="deleteAnno(scope.row.id)">删除公告
                    </el-button>
                </template>
            </el-table-column>


        </el-table>

        <!-- 分页 -->
        <el-pagination :current-page="page" :page-size="limit" :total="total" style="padding: 30px 0; text-align: center;"
            layout="total, prev, pager, next, jumper" @current-change="fetchData" />
    </div>
</template>
<script>
import announcement from '@/api/edu/announcement'

export default {

    data() {
        return {
            listLoading: true, // 是否显示loading信息
            list: null, // 数据列表
            total: 0, // 总记录数
            page: 1, // 页码
            limit: 5, // 每页记录数
            searchObj: {
                priority: '',
                userType: '',
                title: '',
                beginTime: '',
                endTime: '',
            }, // 查询条件

            // 公告优先级选择
            prioritys: [{
                Pvalue: '1',
                label: '一级公告'
            }, {
                Pvalue: '2',
                label: '二级公告'
            }, {
                Pvalue: '3',
                label: '三级公告'
            }],
            Pvalue: '',

            // 老师学生选择
            TeaOrStu: [{
                TSvalue: '1',
                label: '所有老师'
            }, {
                TSvalue: '2',
                label: '所有学生'
            }],
            TSvalue: '',
        }
    },

    created() {
        // 当页面加载时获取数据
        this.fetchData()
        // 获取公告列表
        this.initAnnoList()
    },

    methods: {

        // 点击查询按钮执行的方法
        fetchData(page = 1) { // 调用api层获取数据库中的数据
            // 当点击分页组件的切换按钮的时候，会传输一个当前页码的参数page
            // 解决分页无效问题
            this.page = page
            this.listLoading = true
            this.searchObj.priority=this.Pvalue;
            this.searchObj.userType=this.TSvalue;
            announcement.getPageList(this.page, this.limit, this.searchObj).then(response => {
                console.log("查询对象："+this.searchObj)
                // debugger 设置断点调试
                if (response.success === true) {
                    this.list = response.data.rows
                    this.total = response.data.total
                }
                this.listLoading = false
            })
        },

        // 初始化分类列表
        initAnnoList() {
            subject.getSubjectList().then(response => {
                this.subjectNestedList = response.data.list;
            })
        },

        resetData() {
            this.searchObj = {}
            this.Pvalue=''
            this.TSvalue=''
            this.fetchData()
        },
        // 编辑公告
        editAnnoInfo(annoId) {
            this.$router.push({ path: '/announcement/annopublish/' + annoId })
        },
        // 查询公告信息
        selectAnnoInfo(annoId) {
            this.$router.push({ path: '/announcement/info/' + annoId })
        },
        // 删除公告
        deleteAnno(annoId) {
            this.$confirm('此操作将删除该条公告, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 执行.then说明需要删除，后面执行删除代码
                announcement.deleteAnno(annoId)
                    .then(response => {
                        // 提示信息
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        // 刷新页面
                        this.fetchData();
                    })
            })
        }
    }
}
</script>

<style scoped>
.myClassList .info {
    width: 450px;
    overflow: hidden;
}

.myClassList .info .pic {
    width: 150px;
    height: 90px;
    overflow: hidden;
    float: left;
}

.myClassList .info .pic a {
    display: block;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}

.myClassList .info .pic img {
    display: block;
    width: 100%;
}

.myClassList td .info .title {
    width: 280px;
    float: right;
    height: 90px;
}

.myClassList td .info .title a {
    display: block;
    height: 48px;
    line-height: 24px;
    overflow: hidden;
    color: #00baf2;
    margin-bottom: 12px;
}

.myClassList td .info .title p {
    line-height: 20px;
    margin-top: 5px;
    color: #818181;
}

.caozuo {
    /* transform: scale(0.8); */
}
</style>

   
